﻿using AutoPrint.BJPT.DAL;
using AutoPrint.BJPT.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoPrint.BJPT.BLL
{
    public class EngineService
    {
        /// <summary>
        /// 数据库会话层
        /// </summary>
        private EngineDAL engineDAL = new EngineDAL();

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="eun">二维码</param>
        /// <returns></returns>
        public List<ENGINE> Load(string eun)
        {
            var sql = $"select * from ENGINE where EUN like '%{eun}%' order by ID desc limit 1000";
            var dataList = engineDAL.Query(sql);
            return dataList;
        }



        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="eun">二维码</param>
        /// <returns></returns>
        public List<ENGINE> Load(int date)
        {
            var sql = $"select * from ENGINE where DATE='{date}' order by ID desc";
            var dataList = engineDAL.Query(sql);
            return dataList;
        }

        /// <summary>
        /// 参数化删除
        /// </summary>
        /// <param name="eun"></param>
        /// <returns></returns>
        public int DeleteEun(string eun)
        {
            var sql = $"delete from ENGINE where EUN=@Param";
            var rs = engineDAL.Excute(sql, eun);
            return rs;
        }

        public bool Delete(ENGINE engine)
        {
            var rs = engineDAL.Delete(engine);
            return rs;
        }

        /// <summary>
        /// 更新打印状态
        /// </summary>
        /// <returns></returns>
        public int UpdatePrint(string[] Params)
        {
            var sql = "update ENGINE set PRINTED='是' where EUN in @Params";
            var rs = engineDAL.Excute(sql,Params);
            return rs;
        }

        /// <summary>
        /// 插入二维码数据
        /// </summary>
        /// <param name="engine"></param>
        /// <returns></returns>
        public long Insert(ENGINE engine)
        {
            var id = engineDAL.Insert(engine);
            return id;
        }

        /// <summary>
        /// 查询关闭前未打印数据
        /// </summary>
        /// <returns></returns>
        public List<ENGINE> LoadLastData()
        {
            var sql = "select * from ENGINE where PRINTED='否' order by ID limit 39";
            var dataList = engineDAL.Query(sql);
            return dataList;
        }
    }
}
